package com.example.game1.views;

import java.util.Calendar;
import java.util.Iterator;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v4.util.LruCache;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.game1.managers.GridManager;
import com.example.game1.models.RoomSprite;
import com.example.game1.rooms.Room;

public class DrawingSurface extends SurfaceView
{
	Context	mContext;

	// Resources that we will continually use... keep them here so we dont re-load them every frame refresh
	Bitmap	mBackground;
	int		mBackgroundWidth;
	int		mBackgroundHeight;
	BitmapCache mBitmapCache;
	
	Room mRoom;
	
	// Will be used for drawing
	Rect src = new Rect();
	Rect dest = new Rect();
	
	private int mScreenWidth;
	private int mScreenHeight;

	public DrawingSurface(Context context, AttributeSet attrs) {

		super(context, attrs);
		mContext = context;

		// Use 1/4th of available memory for bitmap caching
		final int memClass = ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
		int maxMemoryCacheSize = 1024 * 1024 * memClass / 4;
		mBitmapCache = new BitmapCache(maxMemoryCacheSize);
		
		
		
	}
	
	public void loadNewResources(Room newRoom)
	{
		mRoom = newRoom;
		
		
		// Load the background bitmap
		mBackground = BitmapFactory.decodeResource(mContext.getResources(), newRoom.getBackgroundResource());
		mBackgroundWidth = mBackground.getWidth();
		mBackgroundHeight = mBackground.getHeight();
		
		// Load bitmaps for any stationary sprites
		Iterator<RoomSprite> i = newRoom.getSprites().iterator();
		while (i.hasNext())
		{
			RoomSprite nextSprite = i.next();
			mBitmapCache.put(nextSprite.getDrawableResource(), BitmapFactory.decodeResource(mContext.getResources(), nextSprite.getDrawableResource()));
		}
		
		
	}

	public void redraw()
	{
		mScreenWidth = getWidth();
		mScreenHeight = getHeight();
		
		// Allows us to control the drawing surface for this frame
		SurfaceHolder sHolder = getHolder();

		// Gives us the 'canvas' to draw onto
		Canvas canvas = sHolder.lockCanvas();
		
		// see if redraw() was called before it was ready
		if (canvas == null)
			return;

		// Tile the room's background image across the screen
		drawBackground(canvas);

		// Now draw any static sprites from the room
		drawRoom(canvas);
		
		
		// TODO draw any moving sprites
		
		// The frame is drawn
		sHolder.unlockCanvasAndPost(canvas);
	}
	
	private void drawBackground(Canvas c)
	{
		src.set(0, 0, mBackgroundWidth, mBackgroundHeight);

		// This tiles our background across the whole screen
		int currentX = 0;
		while (currentX < mScreenWidth)
		{
			int currentY = 0;
			while (currentY < mScreenHeight)
			{
				dest.set(currentX, currentY, currentX + mBackgroundWidth, currentY + mBackgroundHeight);
				c.drawBitmap(mBackground, src, dest, null);
				currentY += mBackgroundHeight;
			}
			currentX += mBackgroundWidth;
		}
	}
	
	private void drawRoom(Canvas c)
	{
		src.set(0, 0, GridManager.SINGLE_GRID_SIZE, GridManager.SINGLE_GRID_SIZE);
		
		// Goes through the list of sprites and draws them
		Iterator<RoomSprite> i = mRoom.getSprites().iterator();
		while (i.hasNext())
		{
			RoomSprite rs = i.next();
			int[] pixelCords = GridManager.getInstance().convertGridToPixels(rs.getXCord(), rs.getYCord());
			dest.set(pixelCords[0], pixelCords[1], pixelCords[0]+GridManager.SINGLE_GRID_SIZE, pixelCords[1]+GridManager.SINGLE_GRID_SIZE);
			c.drawBitmap(mBitmapCache.get(rs.getDrawableResource()), src, dest, null);
		}
	}

	private class BitmapCache extends LruCache<Integer, Bitmap>
	{

		public BitmapCache(int maxSize) {

			super(maxSize);
		}

		@Override
		protected int sizeOf(Integer key, Bitmap value)
		{

			// Return size of bitmaps so LruCache can
			// dynamically adjust cache size
			return value.getRowBytes() * value.getHeight();
		}

		@Override
		protected Bitmap create(Integer key)
		{

			return BitmapFactory.decodeResource(mContext.getResources(), key.intValue());
		}

	}

}
